# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=cmake
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.19.3
pkgrel=1
pkgdesc="A cross-platform open-source make system (mingw-w64)"
arch=('any')
url="https://www.cmake.org/"
license=("MIT")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-bzip2"
             "${MINGW_PACKAGE_PREFIX}-emacs"
             "${MINGW_PACKAGE_PREFIX}-ncurses"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx"
             "${MINGW_PACKAGE_PREFIX}-qt5"
             "${MINGW_PACKAGE_PREFIX}-xz"
             "${MINGW_PACKAGE_PREFIX}-zstd")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-pkg-config"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-jsoncpp"
         "${MINGW_PACKAGE_PREFIX}-libarchive"
         "${MINGW_PACKAGE_PREFIX}-libuv"
         "${MINGW_PACKAGE_PREFIX}-rhash"
         "${MINGW_PACKAGE_PREFIX}-zlib")
optdepends=("${MINGW_PACKAGE_PREFIX}-qt5: CMake Qt GUI"
            "${MINGW_PACKAGE_PREFIX}-emacs: for cmake emacs mode")
options=('staticlibs') # '!strip' 'debug'
source=("https://github.com/Kitware/CMake/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "0001-Windows-Add-missing-stringapiset.h-include.patch"
        "0003-Disable-response-files-for-MSYS-Generator.patch"
        "0005-Do-not-install-Qt-bundle-in-cmake-gui.patch"
        "0006-pkg-config-Add-dont-define-prefix-when-PKG_CONFIG_WI.patch"
        "0007-Do-not-generate-import-libs-for-exes.patch"
        "0008-Output-line-numbers-in-callstacks.patch")
sha256sums=('3faca7c131494a1e34d66e9f8972ff5369e48d419ea8ceaa3dc15b4c11367732'
            '1b68fe64e1b389134db44dc34368713f6e28d3463ab1e7fc3d93857c0459beb7'
            '77763df03e8a9ca66c3f9368fe7e1fc36bb041455733258d73aeb40246a61354'
            '7eb60d12b610c70413412521dcc860d1948c169e721f3525d9be99b3913dc37e'
            'b3905abed55c012108b0254c69cb4027a739f15772500b0a99c400d9dfdf50a7'
            'd85a25cbb4db7ee2729726c59b600a867079d3b0a2e83056f9dc38c6be6e82f6'
            '94642670c922c9acfade79bc567c6b28f2d2f89e27aa391b562e8c90baa49ce1')


# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #


prepare() {
  cd ${_realname}-${pkgver}
  apply_patch_with_msg \
    0001-Windows-Add-missing-stringapiset.h-include.patch \
    0003-Disable-response-files-for-MSYS-Generator.patch \
    0005-Do-not-install-Qt-bundle-in-cmake-gui.patch \
    0006-pkg-config-Add-dont-define-prefix-when-PKG_CONFIG_WI.patch \
    0007-Do-not-generate-import-libs-for-exes.patch \
    0008-Output-line-numbers-in-callstacks.patch
}

build() {
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  MSYSTEM=MINGW FC=${MINGW_PREFIX}/bin/gfortran.exe \
    "${srcdir}"/${_realname}-${pkgver}/configure  \
    --prefix=${MINGW_PREFIX}                      \
    --system-libs                                 \
    --qt-gui                                      \
    --qt-qmake=${MINGW_PREFIX}/bin/qmake.exe      \
    --parallel=${NUMBER_OF_PROCESSORS}            \
    --mandir=share/man                            \
    --docdir=share/doc/cmake                      \
    --sphinx-man --sphinx-html
  plain "Start building..."
  make
}

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  ./bin/ctest.exe -j$(($(nproc)+1))
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  ./bin/cmake.exe \
     -DCMAKE_INSTALL_PREFIX:PATH=${pkgdir}${MINGW_PREFIX} \
     -DCMAKE_INSTALL_LOCAL_ONLY:BOOL=OFF -P cmake_install.cmake
  install -d "${pkgdir}${MINGW_PREFIX}"/share/emacs/site-lisp/
  ${MINGW_PREFIX}/bin/emacs -batch -f batch-byte-compile \
    "${pkgdir}${MINGW_PREFIX}"/share/emacs/site-lisp/cmake-mode.el
}
